import request from '@/utils/request'
import * as URL from '@/constants/api'
import * as util from '@/utils'

// 角色列表
export function getList(params) {
  return request({
    url: URL.ROLE,
    params
  })
    .then(res => res.data.data)
}

// 新增角色
export function add(data) {
  return request({
    url: URL.ROLE,
    method: 'post',
    data
  })
    .then(res => res.data)
}

// 编辑角色
export function edit(data) {
  return request({
    url: `${URL.ROLE}/${data.id}`,
    method: 'put',
    data
  })
    .then(res => res.data)
}

// 删除角色
export function remove(id) {
  return request({
    url: `${URL.ROLE}/${id}`,
    method: 'delete'
  })
    .then(res => res.data)
}

// 角色权限详情
export function getPermission(id) {
  return request({
    url: `${URL.ROLE}/${id}`
  })
    .then(res => res.data.data.permIds)
}

// 角色权限详情
export function setRolePermission(id, idList) {
  console.log(id, idList)
  return request({
    url: URL.ROLE_PERMISSION,
    method: 'put',
    data: {
      id,
      permIds: idList
    }
  })
    .then(res => res.data)
}

// 所有权限信息
export function getPermissionList() {
  return request({
    url: URL.PERMISSION
  })
    .then(res => util.getTreeData(res.data.data))
}
